using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI;
using System.Drawing;
using System.IO;
using WeifenLuo.WinFormsUI.Docking;

namespace Easily
{
    public partial class frmIconBox : DockContent
    {
        #region frmIconBox类的字段
        ///==============================================================================
        /// <summary>当前从树形控件视图中选择的控件的类型</summary>  
        public enum selectedType
        {
            /// <summary>事件控件类型</summary> 
            EventIcon,
            /// <summary>执行控件类型</summary> 
            ExecuteIcon,
            /// <summary>通信控件类型</summary>
            CommunicateIcon,
            /// <summary>传感器控件类型</summary>
            SensorIcon,
            /// <summary>流程控制控件类型</summary>
            FlowControl,
            /// <summary>子程序控件类型</summary>
            SubFunIcon,
            /// <summary>变量控件类型</summary>
            VarIcon,
            /// <summary>无</summary>
            NULL,
        }

        public TreeView IconTreeView
        {
            get { return this.treeViewIconBox; }
            set { this.treeViewIconBox = value; }
        }
        
        public struct SelectedIconFromTreeView
        {
            public selectedType selectedIconType;//当前从树形控件视图中选择的控件的类型
            public int selectedIconNum;//当前从树形控件视图中选择的控件的序号
            public string selectedIconName;//当前从树形控件视图中选择的控件的名字
        };

        public SelectedIconFromTreeView SelectedIconFromTreeViewInfo;

        public TreeNode SelectedIconNode = null;//当前选择的控件图标节点
        public bool AlreadySelectIconFromTreeView;

        private TreeNode EventRootNode;//[WB][20190114]事件根节点
        private TreeNode ExecRootNode;
        private TreeNode CommRootNode;
        private TreeNode SensorRootNode;
        private TreeNode FlowCtrRootNode;
        private TreeNode SubFunRootNode;
        //【20210513】添加变量和应用两个根节点-HSC
        private TreeNode VarRootNode;
        private TreeNode AppRootNode;

        ///==============================================================================
        #endregion

        #region frmIconBox类的方法
        ///==============================================================================

        //frmIconBox构造函数
        public frmIconBox()
        {
            InitializeComponent();
            AlreadySelectIconFromTreeView = false;
            if (!LoadIconFromIconBase())     
            {
                MessageBox.Show("生成图标控件箱失败!");
            }
        }

        //从图标控件箱中导出图标控件信息形成图标控件箱
        private bool LoadIconFromIconBase()
        {
            try
            {
                //[2018]下标改为变量
                int i = 0;
                //首先创建4个根节点,分别为"执行控件","传感器控件","流程控制"和"子程序"
                EventRootNode = new TreeNode("事件控件");
                this.treeViewIconBox.Nodes.Add(EventRootNode);
                //this.treeViewIconBox.Nodes[i++].ImageIndex = this.imageListIconBox.Images.IndexOfKey("执行控件2.png");
                this.treeViewIconBox.Nodes[i++].ImageIndex = this.imageListIconBox.Images.IndexOfKey("事件控件.png"); 
 

                ExecRootNode = new TreeNode("执行控件");
                this.treeViewIconBox.Nodes.Add(ExecRootNode);
                //this.treeViewIconBox.Nodes[i++].ImageIndex = this.imageListIconBox.Images.IndexOfKey("执行控件2.png");
                this.treeViewIconBox.Nodes[i++].ImageIndex = this.imageListIconBox.Images.IndexOfKey("执行控件.png");

                SensorRootNode = new TreeNode("传感器控件");
                this.treeViewIconBox.Nodes.Add(SensorRootNode);
                this.treeViewIconBox.Nodes[i++].ImageIndex = this.imageListIconBox.Images.IndexOfKey("传感器控件.png");


                CommRootNode = new TreeNode("通信控件");
                this.treeViewIconBox.Nodes.Add(CommRootNode);
                this.treeViewIconBox.Nodes[i++].ImageIndex = this.imageListIconBox.Images.IndexOfKey("通信控件.png");

                FlowCtrRootNode = new TreeNode("流程控件");
                this.treeViewIconBox.Nodes.Add(FlowCtrRootNode);
                this.treeViewIconBox.Nodes[i++].ImageIndex = this.imageListIconBox.Images.IndexOfKey("流程控件.png");
                              
                
                SubFunRootNode = new TreeNode("子程序控件");
                SubFunRootNode.Name = "SubFunction";
                this.treeViewIconBox.Nodes.Add(SubFunRootNode);
                this.treeViewIconBox.Nodes[i++].ImageIndex = this.imageListIconBox.Images.IndexOfKey("子程序控件.png");

                //【20210513】在ImageList控件中加入变量.png和应用.png
                //（这两个图片的路径存放在...\bin\Debug\Resource\IconBox\Root）
                //依照上面添加的方法，在树中添加两个新的节点
                VarRootNode = new TreeNode("变量控件");
                VarRootNode.Name = "Variable";
                this.treeViewIconBox.Nodes.Add(VarRootNode);
                this.treeViewIconBox.Nodes[i++].ImageIndex = this.imageListIconBox.Images.IndexOfKey("变量.png");

              


                //创建叶子图标控件
                if (!LoadSubNode())
                {
                    return false;
                }
               // this.treeViewIconBox.ExpandAll();//展开树形视图151129
                this.treeViewIconBox.SelectedNode = null;//初始化未选中节点
                SelectedIconNode = ExecRootNode;//初始化当前选中的节点为"执行控件"根节点
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        //导出根的叶子图标控件
        private bool LoadSubNode()
        {
            try
            {
                foreach(var v in Globaldefine.loadConfig.controlIconDic)
                {
                    if (v.Value.type == "事件控件")
                    {
                        if (!LoadTreeNodeNameAndImage(EventRootNode, v.Key))
                        {
                            return false;
                        }
                    }
                    else if (v.Value.type == "传感器控件")
                    {
                        if (!LoadTreeNodeNameAndImage(SensorRootNode, v.Key))
                        {
                            return false;
                        }
                    }
                    else if(v.Value.type == "执行控件")
                    {
                        if(v.Key != "开始")
                        {
                            if (!LoadTreeNodeNameAndImage(ExecRootNode, v.Key))
                            {
                                return false;
                            }
                        }
                    }
                    else if (v.Value.type == "通信控件")
                    {
                        if (!LoadTreeNodeNameAndImage(CommRootNode, v.Key))
                        {
                            return false;
                        }
                    }
                    else if(v.Value.type == "流程控件")
                    {
                        if(!v.Key.Contains("End"))
                        {
                            if (!LoadTreeNodeNameAndImage(FlowCtrRootNode, v.Key))
                            {
                                return false;
                            }
                        }
                    }
                    else if (v.Value.type == "子程序控件")
                    {
                        if (!LoadTreeNodeNameAndImage(SubFunRootNode, v.Key))
                        {
                            return false;
                        }
                    }
                    //【20210513】为两个根节点添加子节点
                    else if (v.Value.type == "变量控件")
                    {
                        if (!LoadTreeNodeNameAndImage(VarRootNode, v.Key))
                        {
                            return false;
                        }
                    }
                    else if (v.Value.type == "应用控件")
                    {
                        if (!LoadTreeNodeNameAndImage(AppRootNode, v.Key))
                        {
                            return false;
                        }
                    }
                }
                return true;

            }
            catch
            {
                return false;
            }
        }

        //设置叶子图标控件
        private bool LoadTreeNodeNameAndImage(TreeNode RootNode, string name )
        {
            Image img;
            //int i;
            
            try
            {
                img = System.Drawing.Image.FromFile(Globaldefine.exePath + @"Resource\IconImage\Execute\" + 
                    Globaldefine.loadConfig.controlIconDic[name].image1);
                imageListIconBox.Images.Add(img);//添加节点图片
                //创建子节点,并关联子节点图片
                TreeNode node = new TreeNode();
                //node.Text = Globaldefine.FrmParent.InfoOfIconBox.controlIconDic[name].controlImage2;
                node.Text = name;
                RootNode.Nodes.Add(node);
                node.ImageIndex = imageListIconBox.Images.Count - 1;//减一，由于图片索引从0开始
                node.SelectedImageIndex = imageListIconBox.Images.Count - 1;
                //node.
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        ///==============================================================================
        #endregion

        #region frmIconBox类的事件
        ///==============================================================================

        //树形控件节点选择
        private void treeViewIconBox_AfterSelect(object sender, TreeViewEventArgs e)
        {
             //20151129-30
           if (e.Node.Level.Equals(1))
           {
               if (e.Node.Parent.Text == "事件控件")
               {
                   SelectedIconFromTreeViewInfo.selectedIconType = selectedType.EventIcon;
                   SelectedIconFromTreeViewInfo.selectedIconName = e.Node.Text; //实际用的是selectedIconName来确定所选控件
                   AlreadySelectIconFromTreeView = true;//置单击标志
                   SelectedIconNode = e.Node;//标记该节点被选中，其他代码中未使用该信息，下同。
                   this.treeViewIconBox.Cursor = System.Windows.Forms.Cursors.Hand;
               }
               else if (e.Node.Parent.Text == "执行控件")
                {
                    SelectedIconFromTreeViewInfo.selectedIconType = selectedType.ExecuteIcon;
                    SelectedIconFromTreeViewInfo.selectedIconName = e.Node.Text; //实际用的是selectedIconName来确定所选控件
                    AlreadySelectIconFromTreeView = true;//置单击标志
                    SelectedIconNode = e.Node;//标记该节点被选中，其他代码中未使用该信息，下同。
                    this.treeViewIconBox.Cursor = System.Windows.Forms.Cursors.Hand;
                }
                else if (e.Node.Parent.Text == "传感器控件")//20151129
                {
                    SelectedIconFromTreeViewInfo.selectedIconType = selectedType.SensorIcon;
                    SelectedIconFromTreeViewInfo.selectedIconNum = e.Node.Index + 1;
                    SelectedIconFromTreeViewInfo.selectedIconName = e.Node.Text;
                    AlreadySelectIconFromTreeView = true;//置单击标志
                    SelectedIconNode = e.Node;
                    this.treeViewIconBox.Cursor = System.Windows.Forms.Cursors.Hand;
                }
                if (e.Node.Parent.Text == "通信控件")
                {
                    SelectedIconFromTreeViewInfo.selectedIconType = selectedType.CommunicateIcon;
                    SelectedIconFromTreeViewInfo.selectedIconNum = e.Node.Index + 1;
                    SelectedIconFromTreeViewInfo.selectedIconName = e.Node.Text;
                    AlreadySelectIconFromTreeView = true;//置单击标志
                    SelectedIconNode = e.Node;
                    this.treeViewIconBox.Cursor = System.Windows.Forms.Cursors.Hand;
                }
                if (e.Node.Parent.Text == "流程控件")
                {
                    SelectedIconFromTreeViewInfo.selectedIconType = selectedType.FlowControl;
                    SelectedIconFromTreeViewInfo.selectedIconNum = e.Node.Index + 1;
                    SelectedIconFromTreeViewInfo.selectedIconName = e.Node.Text;
                    AlreadySelectIconFromTreeView = true;//置单击标志
                    SelectedIconNode = e.Node;
                    this.treeViewIconBox.Cursor = System.Windows.Forms.Cursors.Hand;
                }
                if (e.Node.Parent.Text == "子程序控件")
                {
                        SelectedIconFromTreeViewInfo.selectedIconType = selectedType.SubFunIcon;
                    SelectedIconFromTreeViewInfo.selectedIconNum = e.Node.Index + 1;
                    SelectedIconFromTreeViewInfo.selectedIconName = e.Node.Text;
                    AlreadySelectIconFromTreeView = true; //置单击标志
                    SelectedIconNode = e.Node;
                    this.treeViewIconBox.Cursor = System.Windows.Forms.Cursors.Hand;
                }
                //TODO【20210513】为子节点设置点击事件
                if (e.Node.Parent.Text == "变量控件")
                {
                    SelectedIconFromTreeViewInfo.selectedIconType = selectedType.VarIcon;
                    SelectedIconFromTreeViewInfo.selectedIconNum = e.Node.Index + 1;
                    SelectedIconFromTreeViewInfo.selectedIconName = e.Node.Text;
                    AlreadySelectIconFromTreeView = true;//置单击标志
                    SelectedIconNode = e.Node;
                    this.treeViewIconBox.Cursor = System.Windows.Forms.Cursors.Hand;
                }
            }
             else
             {
                SelectedIconNode = null;
                SelectedIconFromTreeViewInfo.selectedIconType = selectedType.NULL;
                AlreadySelectIconFromTreeView = false;
                this.treeViewIconBox.Cursor = System.Windows.Forms.Cursors.Default;
             }
           this.treeViewIconBox.SelectedNode = null;//初始化未选中节点
              
        }

        private void frmIconBox_Load(object sender, EventArgs e)
        {

        }

        ///==============================================================================
        #endregion
    }
}